package org.br.erp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.br.erp.base.entity.Scmprintdefrelation;
import org.br.erp.base.mapper.ScmprintdefrelationMapper;
import org.br.erp.base.service.IScmprintdefrelationService;
import org.br.erp.base.vo.ScmprintdefRelVo;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.system.vo.SysDepartModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description: 打印格式分配关系
 * @Author: jeecg-boot
 * @Date:   2023-01-30
 * @Version: V1.0
 */
@Service
public class ScmprintdefrelationServiceImpl extends ServiceImpl<ScmprintdefrelationMapper, Scmprintdefrelation> implements IScmprintdefrelationService {

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private ScmprintdefrelationMapper scmprintdefrelationMapper;
    @Override
    public List<ScmprintdefRelVo> queryPrintDefRelVoInfoByPrintdefid(String printdefid) {
        QueryWrapper<Scmprintdefrelation> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("printdefid",printdefid);
        List<Scmprintdefrelation> scmprintdefrelations = scmprintdefrelationMapper.selectList(queryWrapper);
        List<ScmprintdefRelVo> res=new ArrayList<>();
        if(scmprintdefrelations!=null && scmprintdefrelations.size()>0){
            for (Scmprintdefrelation scmprintdefrelation : scmprintdefrelations) {
                String companyid = scmprintdefrelation.getCompanyid();
                String printuserid = scmprintdefrelation.getPrintuserid();
                String companyname="";
                String username="";
                ScmprintdefRelVo scmprintdefRelVo=new ScmprintdefRelVo();

                scmprintdefRelVo.setPrintuserid(printuserid);
                scmprintdefRelVo.setId(scmprintdefrelation.getId());
                scmprintdefRelVo.setPrintdefid(scmprintdefrelation.getPrintdefid());
                BeanUtils.copyProperties(scmprintdefrelation,scmprintdefRelVo);
                if(companyid!=null){
                    List<SysDepartModel> collect = sysBaseAPI.getAllSysDepart().stream()
                            .filter(item -> item.getId().equals(companyid)).collect(Collectors.toList());
                    if( collect.size() >0){
                        scmprintdefRelVo.setCompanyname(collect.get(0).getDepartName());
                    }
                }
                if(printuserid!=null){
                    LoginUser userById = sysBaseAPI.getUserById(printuserid);
                    if(userById!=null){
                        scmprintdefRelVo.setUsername(userById.getRealname());
                    }
                }
                res.add(scmprintdefRelVo);
            }
            return res;
        }else{
            return null;
        }
    }
}
